package ua.pp.bizon.yandex.nb.model;

import java.util.Enumeration;
import java.util.Iterator;

import javax.swing.tree.TreeNode;

import ua.pp.bizon.yandex.api.IAppService;
import ua.pp.bizon.yandex.api.IJobExecutor;
import ua.pp.bizon.yandex.api.Job;
import ua.pp.bizon.yandex.api.ObjectLookup;
import ua.pp.bizon.yandex.api.impl.EventSupport;
import ua.pp.bizon.yandex.api.impl.EventSupport.ChangeListener;
import ua.pp.bizon.yandex.uploader.atom.AlbumsList;
import ua.pp.bizon.yandex.uploader.atom.AppService;
import ua.pp.bizon.yandex.uploader.atom.EntryAlbum;

public class RemoteNode extends EventSupport implements TreeNode, ChangeListener {

	private AlbumsList list;

	public RemoteNode(AlbumsList albumsList) {
		this.list = albumsList;
	}

	public RemoteNode() {
		this.list = null;
		ObjectLookup.lookup(IJobExecutor.class).exec(new Job() {
			@Override
			public void run() {
				AppService appService = ObjectLookup.lookup(IAppService.class).get();
				if (appService == null) {
					throw new NullPointerException("AppService is NULL");
				}
				list = appService.getAlbumsList();
				tourchListeners();
			}

			@Override
			public String toString() {
				return "Remote Node Getter";
			}
		});
	}

	@Override
	public TreeNode getChildAt(int childIndex) {
		return new RemoteAlbumNode(list.get(childIndex), this);
	}

	@Override
	public int getChildCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public TreeNode getParent() {
		return null;
	}

	@Override
	public int getIndex(TreeNode node) {
		return list.indexOf(((RemoteNode) node).list);
	}

	@Override
	public boolean getAllowsChildren() {
		return true;
	}

	@Override
	public boolean isLeaf() {
		return false;
	}

	@Override
	public Enumeration<RemoteNode> children() {
		if (list == null) {
			return new Enumeration<RemoteNode>() {
				@Override
				public boolean hasMoreElements() {
					return false;
				}

				@Override
				public RemoteNode nextElement() {
					return null;
				}
			};

		}
		return new Enumeration<RemoteNode>() {
			Iterator<EntryAlbum> iterator = list.iterator();

			@Override
			public boolean hasMoreElements() {
				return iterator.hasNext();
			}

			@Override
			public RemoteNode nextElement() {
				return new RemoteAlbumNode(iterator.next(), RemoteNode.this);
			}

		};
	}

	@Override
	public String toString() {
		return list == null ? "Please wait" : list.getName();
	}

	public AlbumsList getAlbum() {
		return list;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof RemoteNode)) {
			return false;
		}
		RemoteNode n = (RemoteNode) obj;
		if (list == null) {
			return n.list == null;
		}
		return list.equals(((RemoteNode) obj).list);
	}

	@Override
	public int hashCode() {
		return list == null ? 0 : list.hashCode();
	}

	@Override
	public void dataChanged() {
		list = ObjectLookup.lookup(IAppService.class).get().getAlbumsList();
	}

}
